<template>
	<el-dialog :title="form.orderId ? '编辑' : '新增'" v-model="visible" :close-on-click-modal="false" draggable>
		<el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
			<el-row :gutter="24">
				<el-col :span="12" class="mb20">
					<el-form-item label="交易流水号" prop="orderNo">
						<el-input v-model="form.orderNo" placeholder="请输入交易流水号" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="交易类型" prop="orderType">
						<el-input v-model="form.orderType" placeholder="请输入交易类型" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="买家id" prop="buyerId">
						<el-input v-model="form.buyerId" placeholder="请输入买家id" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="买家名称" prop="buyerName">
						<el-input v-model="form.buyerName" placeholder="请输入买家名称" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="买家uid" prop="buyerUid">
						<el-input v-model="form.buyerUid" placeholder="请输入买家uid" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="商户ID" prop="merchantId">
						<el-input v-model="form.merchantId" placeholder="请输入商户ID" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="商户号" prop="mchNo">
						<el-input v-model="form.mchNo" placeholder="请输入商户号" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="商户名称" prop="mchName">
						<el-input v-model="form.mchName" placeholder="请输入商户名称" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="商户应用ID" prop="appId">
						<el-input v-model="form.appId" placeholder="请输入商户应用ID" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="应用ID" prop="appNo">
						<el-input v-model="form.appNo" placeholder="请输入应用ID" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="应用名称" prop="appName">
						<el-input v-model="form.appName" placeholder="请输入应用名称" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="商户订单号" prop="mchOrderNo">
						<el-input v-model="form.mchOrderNo" placeholder="请输入商户订单号" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="商品标题" prop="subject">
						<el-input v-model="form.subject" placeholder="请输入商品标题" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="商品描述信息" prop="body">
						<el-input v-model="form.body" placeholder="请输入商品描述信息" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="订单金额" prop="orderAmount">
						<el-input v-model="form.orderAmount" placeholder="请输入订单金额" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="卡券金额" prop="couponAmount">
						<el-input v-model="form.couponAmount" placeholder="请输入卡券金额" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="服务费" prop="feeAmount">
						<el-input v-model="form.feeAmount" placeholder="请输入服务费" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="实付金额" prop="payAmount">
						<el-input v-model="form.payAmount" placeholder="请输入实付金额" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="退款次数" prop="refundCnt">
						<el-input v-model="form.refundCnt" placeholder="请输入退款次数" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="退款总金额" prop="refundAmount">
						<el-input v-model="form.refundAmount" placeholder="请输入退款总金额" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="退款状态" prop="refundStatus">
						<el-input v-model="form.refundStatus" placeholder="请输入退款状态" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="货币代码" prop="currency">
						<el-input v-model="form.currency" placeholder="请输入货币代码" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="成功跳转地址" prop="redirectUrl">
						<el-input v-model="form.redirectUrl" placeholder="请输入成功跳转地址" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="失败跳转地址" prop="backUrl">
						<el-input v-model="form.backUrl" placeholder="请输入失败跳转地址" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="异步通知地址" prop="notifyUrl">
						<el-input v-model="form.notifyUrl" placeholder="请输入异步通知地址" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="来源编码" prop="srcCd">
						<el-input v-model="form.srcCd" placeholder="请输入来源编码" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="付款码" prop="payErcode">
						<el-input v-model="form.payErcode" placeholder="请输入付款码" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="客户端IP" prop="clientIp">
						<el-input v-model="form.clientIp" placeholder="请输入客户端IP" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="付款设备" prop="clientDevice">
						<el-input v-model="form.clientDevice" placeholder="请输入付款设备" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="商户设备" prop="mchDevice">
						<el-input v-model="form.mchDevice" placeholder="请输入商户设备" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="扩展信息" prop="extInfo">
						<el-input v-model="form.extInfo" placeholder="请输入扩展信息" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="订单失效时间" prop="expiredTime">
						<el-input v-model="form.expiredTime" placeholder="请输入订单失效时间" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="支付成功时间" prop="successTime">
						<el-input v-model="form.successTime" placeholder="请输入支付成功时间" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="订单备注" prop="remark">
						<el-input v-model="form.remark" placeholder="请输入订单备注" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="关联订单id" prop="relaOrderId">
						<el-input v-model="form.relaOrderId" placeholder="请输入关联订单id" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="回调状态" prop="notifyStatus">
						<el-input v-model="form.notifyStatus" placeholder="请输入回调状态" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="状态" prop="status">
						<el-input v-model="form.status" placeholder="请输入状态" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="创建时间" prop="createAt">
						<el-input v-model="form.createAt" placeholder="请输入创建时间" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="更新时间" prop="updateAt">
						<el-input v-model="form.updateAt" placeholder="请输入更新时间" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="租户" prop="tanentId">
						<el-input v-model="form.tanentId" placeholder="请输入租户" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">取 消</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">确 认</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="PayOrderDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from '/@/hooks/message';
import { getObj, addObj, putObj, validateExist } from '/@/api/payment/PayOrder';
import { rule } from '/@/utils/validate';
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);
// 定义字典

// 提交表单数据
const form = reactive({
	orderId: '',
	orderNo: '',
	orderType: '',
	buyerId: '',
	buyerName: '',
	buyerUid: '',
	merchantId: '',
	mchNo: '',
	mchName: '',
	appId: '',
	appNo: '',
	appName: '',
	mchOrderNo: '',
	subject: '',
	body: '',
	orderAmount: '',
	couponAmount: '',
	feeAmount: '',
	payAmount: '',
	refundCnt: '',
	refundAmount: '',
	refundStatus: '',
	currency: '',
	redirectUrl: '',
	backUrl: '',
	notifyUrl: '',
	srcCd: '',
	payErcode: '',
	clientIp: '',
	clientDevice: '',
	mchDevice: '',
	extInfo: '',
	expiredTime: '',
	successTime: '',
	remark: '',
	relaOrderId: '',
	notifyStatus: '',
	status: '',
	createAt: '',
	updateAt: '',
	tanentId: '',
});

// 定义校验规则
const dataRules = ref({});

// 打开弹窗
const openDialog = (id: string) => {
	visible.value = true;
	form.orderId = '';

	// 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});

	// 获取payOrder信息
	if (id) {
		form.orderId = id;
		getPayOrderData(id);
	}
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
		loading.value = true;
		form.orderId ? await putObj(form) : await addObj(form);
		useMessage().success(form.orderId ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};

// 初始化表单数据
const getPayOrderData = (id: string) => {
	// 获取数据
	loading.value = true;
	getObj({ orderId: id })
		.then((res: any) => {
			Object.assign(form, res.data[0]);
		})
		.finally(() => {
			loading.value = false;
		});
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
